package com.csdig.cms.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.csdig.cms.dao.ContentCollectDAO;
import com.csdig.cms.model.ContentCollect;
import com.csdig.db.DBUtils;
import com.csdig.db.model.Pagination;
import com.csdig.db.model.Sql;

@Repository
public class ContentCollectDAOImpl implements ContentCollectDAO {

	@Override
	public void add(ContentCollect vo) throws Exception {
		Sql sql = new Sql("insert into content_collect(id,url,title,summary,content,create_date,state,channel_type)");
		sql.append("values(:id,:url,:title,:summary,:content,:create_date,:state,:channel_type)");
		sql.set("id", vo.getId());
		sql.set("url", vo.getUrl());
		sql.set("title", vo.getTitle());
		sql.set("summary", vo.getSummary());
		sql.set("content", vo.getContent());
		sql.set("create_date", vo.getCreateDate());
		sql.set("state", vo.getState());
		sql.set("channel_type", vo.getChannelType());
		DBUtils.exeUpdate(sql);
	}

	@Override
	public void update(ContentCollect vo) throws Exception {
		String sql = "update content_collect set url=?,title=?,summary=?,content=?,create_date=?,state=?,channel_type=?  where id=?";
		DBUtils.exeUpdate(
				sql,
				new Object[] { vo.getUrl(), vo.getTitle(), vo.getSummary(), vo.getContent(), vo.getCreateDate(),
						vo.getState(), vo.getChannelType(), vo.getId() });
	}

	@Override
	public void delete(java.lang.Integer id) throws Exception {
		String sql = "delete from content_collect where id=?";
		DBUtils.exeUpdate(sql, new Object[] { id });
	}

	@Override
	public ContentCollect findById(java.lang.Integer id) throws Exception {
		String sql = "select * from content_collect where id=?";
		return DBUtils.queryBean(sql, ContentCollect.class, id);
	}

	@Override
	public Pagination<ContentCollect> findByCondition(ContentCollect vo, int pageNo, int pageSize) throws Exception {
		Sql sql = new Sql("select * from content_collect where 1=1 ");
		// /add condtion here
		return DBUtils.queryPagination(sql, null, pageNo, pageSize, ContentCollect.class);
	}

	@Override
	public List<ContentCollect> findByCondition(ContentCollect vo) throws Exception {
		Sql sql = new Sql("select * from content_collect where 1=1 ");
		// /add condtion here
		if (vo != null) {

		}
		return DBUtils.queryBeanList(sql, ContentCollect.class);
	}

	@Override
	public List<ContentCollect> listAll() throws Exception {
		return findByCondition(null);
	}

	@Override
	public ContentCollect findbyUrl(String url) throws Exception {
		Sql sql = new Sql("select * from content_collect where url=:url ");
		sql.set("url", url);
		return DBUtils.queryBean(sql, ContentCollect.class);
	}

	@Override
	public List<ContentCollect> findByCondition(String[] fields, Object[] values,String orderby) throws Exception {
		Sql sql = new Sql("select * from content_collect where 1=1 ");
		for (int i = 0; i < fields.length; i++) {
			sql.append(" and " + fields[i] + "=:" + fields[i]);
			sql.set(fields[i], values[i]);
		}
		if(StringUtils.isNotEmpty(orderby)){
			sql.append(" order by "+orderby);
		}
		
		return DBUtils.queryBeanList(sql, ContentCollect.class);
	}

}
